// packagePay/pages/new-select-list/new-select-list.js
import {
    getProjectList,
    payCopyWorkerProject
} from '../../../api/app'
import {
    addFromProject,
    addeleSignProject
} from '../../api/insurance'
import user from '../../../utils/user'
import {
    shake
} from '../../../utils/util'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        selectProjectId: '',
        searchText: '',
        tabs: [
            '全部',
            '无打卡',
            '需打卡'
        ],
        current: 0,
        pageNum: 1,
        isDelete: false,
        hasNextPage: false,
        list: [],
        checkedId: '',
        checkedPeopleNum: '',
        projectId: '',
        idS: [],
        requestType: '',
        dataPayCallback: ''
    },
    onLoad(options) {
        console.log(options, 'options')
        this.setData({
            list: [],
            pageNum: 1,
            hasNextPage: false,
            projectId: options.id,
            requestType: options.type
        })
        this.getProjectList()
    },
    searchWorker(event) {
        shake(() => {
            const {
                value
            } = event.detail
            this.setData({
                searchText: value,
                list: [],
                pageNum: 1,
                hasNext: true
            })
            this.getProjectList()
        })
    },
    onShow() {
        
    },
    onUnload() {
        if (this.data.dataPayCallback) {
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.emit('dataCallback', {
                data: this.data.dataPayCallback
            })
        }
    },
    // onReachBottom() {
    //     console.log('到达底部啦')
    //     if(!this.data.hasNextPage){
    //         return
    //     }
    //     this.setData({
    //         pageNum:this.data.pageNum += 1
    //     })
    //     console.log(this.data.pageNum,'pageNum')
    //     this.getProjectList()
    // },
    cancel() {
        this.setData({
            isDelete: false
        })
    },
    showDeterminePopup() {
        if (!this.data.checkedId) {
            app.toast('请至少选择一个')
            return
        }
        const arr = this.data.list.map((item, index) => {
            if (item.checked) {
                return item.peopleNum
            }
        }).filter(n => n)
        console.log(arr, 'arr')
        // if(arr.length === 0){
        // 	app.toast('该项目中无员工')
        // 	return
        // }
        this.setData({
            isDelete: true,
            checkedPeopleNum: arr[0] ? arr[0] : 0
        }, () => {
            // setTimeout(()=>{
            //     wx.navigateBack()
            // },1000)
        })
    },
    async onAddPersonnel() {
        if (this.data.requestType === 'pay') {
            await payCopyWorkerProject({
                id: this.data.projectId,
                ids: this.data.idS
            })
            this.cancel()
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.emit('dataCallback', {
                data: 'payDdata'
            })
            await app.toast('人员导入成功')
            await wx.navigateBack()

        } else if (this.data.requestType === 'eleSign') {
            await addeleSignProject({
                projectId: this.data.projectId,
                oldProjectId: this.data.selectProjectId
            })
            this.cancel()
            await app.toast('人员导入成功')
            await wx.navigateBack()
        } else {
            await addFromProject({
                id: this.data.projectId,
                ids: this.data.idS
            })
            this.cancel()
            await app.toast('人员导入成功')
            await wx.navigateBack()
        }
    },
    switchtab(event) {
        this.setData({
            current: event.currentTarget.dataset.index,
            pageNum: 1,
            list: []
        })
        this.getProjectList()
    },
    // 选择单个
    onChecked(event) {
        console.log(event, 'event')
        const {
            id
        } = event.currentTarget.dataset
        this.data.list.forEach((item, index) => {
            if (item.id == this.data.checkedId) {
                this.setData({
                    checkedId: '',
                    ['list[' + index + '].checked']: false
                })
            }
        })
        this.data.list.forEach((item, index) => {
            if (item.id == id) {
                this.setData({
                    checkedId: id,
                    ['list[' + index + '].checked']: true
                })
            }
        })
        console.log(this.data.list, this.data.checkedId, '打印信息')
        const arr = []
        arr.push(this.data.checkedId)
        this.setData({
            idS: arr,
            selectProjectId: id
        })
        console.log(arr, 'arr')
    },
    // 到达底部事件
    onScroll() {
        console.log('到达底部啦')
        if (!this.data.hasNextPage) {
            return
        }
        this.setData({
            pageNum: this.data.pageNum += 1
        })
        console.log(this.data.pageNum, 'pageNum')
        this.getProjectList()
    },
    // 获取数据
    async getProjectList() {
        // 改接口不需要搜索 后期可能添加
        const result = await getProjectList({
            companyId: user.companyId,
            type: this.data.current ? (this.data.current - 1) : null,
            params: {
                userId: user.userId
            },
            pageNum: this.data.pageNum,
            pageSize: 10,
            searchName: this.data.searchText,
            requestType: this.data.requestType ? '1' : ''
        })
        const arr = this.data.list
        arr.push(...result.list)
        this.setData({
            list: arr,
            hasNextPage: result.hasNextPage
        })
        console.log(this.data.list, 'listlist')
        console.log(result, 'result')
    },
    goProjectDetail(event) {
        console.log(event, 'event')
        const {
            id
        } = event.currentTarget.dataset
        wx.navigateTo({
            url: '/packageInsurance/pages/project-detail-import/project-detail-import?id=' + this.data.projectId + '&ids=' + id + '&type=' + (this.data.requestType || ''),
            events: {
                dataCallback: (data) => {
                    this.setData({
                        dataPayCallback: data.data
                    })
                    console.log(data, '页面c回来的数据')
                },
            }
        })
    },
    // 发薪添加项目
    async onPayCopyWorkerProject() {
        console.log(result, 'result')
    }
})